package com.byq.mm.smartlib.cluster.activity.client;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.byq.mm.smartlib.R;


import com.byq.mm.abstractx.cluster.client.event.ClusterChangeEvent;
import com.byq.mm.application.cluster.client.TunnelClient;
import com.byq.mm.foundation.cluster.client.event.AttributeEvent;
import com.byq.mm.foundation.cluster.client.event.SubscribeEvent;
import com.byq.mm.foundation.cluster.datatype.ByteArrayValue;
import com.byq.mm.smartlib.obd.ObdSensorAdapter;
import com.byq.mm.smartlib.obd.commands.decoder.CommandDecoder;
import com.byq.mm.smartlib.obd.commands.decoder.DecoderManager;
import com.byq.mm.smartlib.component.SmartGestureListener;

public class TunnelActivity extends ClientClusterActivity<TunnelClient> {
//	ImageButton onoff;

	Button refresh;
	private Button startTrace;
	private Button stopTrace;
	DecoderManager decoderManager = new DecoderManager();
	List<ObdSensorAdapter> adapters = new ArrayList<ObdSensorAdapter>();
	View[] containers = new View[6];
	GridView[] grids = new GridView[6];
	TextView[] titles = new TextView[6];

	ViewFlipper flipper;

	private GestureDetector gesture;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.cluster_tunnel);
		startTrace = (Button) findViewById(R.id.startTrace);
		startTrace.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			
				client.subscribe();
			}
		});
		stopTrace = (Button) findViewById(R.id.stopTrace);
		stopTrace.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				client.unsubscribe();
			}
		});
		flipper = (ViewFlipper) findViewById(R.id.obdFlipper);
		String path = getPackageName();
		for (int i = 0; i < 6; i++) {
			int index = i + 1;

			int containerId = getResources().getIdentifier("container" + index,
					"id", path);
			containers[i] = findViewById(containerId);

			int gridId = getResources().getIdentifier("grid" + index, "id",
					path);
			grids[i] = (GridView) findViewById(gridId);
			int titleId = getResources().getIdentifier("grouptitle" + index,
					"id", path);
			titles[i] = (TextView) findViewById(titleId);
		}

		int i = 0;
		for (String group : decoderManager.groups()) {
			Map<String, CommandDecoder> ds = decoderManager.getGroupedDecoders(group);
			List<Map<String, CommandDecoder>> splitted = decoderManager.split(ds);
			for (Map<String, CommandDecoder> dd : splitted) {
				if (i >= containers.length)
					break;
				ObdSensorAdapter sgp = new ObdSensorAdapter(this, dd);
				grids[i].setAdapter(sgp);
				titles[i].setText(group);
				i++;

			}

		}

		gesture = new GestureDetector(new SmartGestureListener(flipper));

	}
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		boolean b = super.dispatchTouchEvent(ev);
		//if (!b)
			gesture.onTouchEvent(ev);
		return b;

	}
	@Override
	protected void update(ClusterChangeEvent e) {
		
			if(e instanceof AttributeEvent){
				ByteArrayValue value = client.getData();
				byte[] data = value.getValue();
				decoderManager.decode(data);
				for(ObdSensorAdapter ad : adapters)
					ad.update();
			}
			if(e instanceof SubscribeEvent){
			
				boolean subscribed = client.isSubscribed();
				this.startTrace.setEnabled(!subscribed);
				this.stopTrace.setEnabled(subscribed);
			}

		
	}

}